//
//  DigitalInputView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/2.
//

#import "DigitalInputView.h"
#import "TemplateFoundDetailsModel.h"

@interface DigitalInputView ()<UITextFieldDelegate>

///减号
@property (nonatomic ,strong) UIButton * reduceBtn;
///加号
@property (nonatomic ,strong) UIButton * plusBtn;
@property (nonatomic ,strong) UITextField * textField;
@end

@implementation DigitalInputView

+ (instancetype)NewView {
    DigitalInputView * view = [DigitalInputView new];
    view.clipsToBounds = YES;
    //    view.backgroundColor = UIColor.whiteColor;
    [view newUI];
    return view;
}
+ (instancetype)DKNewView{
    DigitalInputView * view = [DigitalInputView NewView];
    [view.plusBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(32));
    }];
    [view.reduceBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(32));
    }];
    [view.textField mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(32));
    }];
    return view;
}

- (void)newUI{
    MJWeakSelf
    
    UIButton * reduceBtn = [UIButton buttonWithType:0];
    self.reduceBtn = reduceBtn;
    [reduceBtn setImage:[UIImage imageNamed:@"template_reduce1"] forState:UIControlStateNormal];
    [reduceBtn setImage:[UIImage imageNamed:@"template_reduce2"] forState:UIControlStateDisabled];
    [self addSubview:reduceBtn];
    [reduceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.centerY.offset(0);
        make.width.height.offset(kwt(22));
    }];
    reduceBtn.addTarget(self,@selector(reduceBtnClick));
    
    UIButton * plusBtn = [UIButton buttonWithType:0];
    self.plusBtn = plusBtn;
    [plusBtn setImage:[UIImage imageNamed:@"template_plus1"] forState:UIControlStateNormal];
    [plusBtn setImage:[UIImage imageNamed:@"template_plus2"] forState:UIControlStateDisabled];
    [self addSubview:plusBtn];
    [plusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(0);
        make.centerY.offset(0);
        make.width.height.offset(kwt(22));
    }];
    plusBtn.addTarget(self,@selector(plusBtnClick));
    
    UITextField * textField = [UITextField new];
    textField.backgroundColor = UIColor.whiteColor;
    textField.textAlignment = NSTextAlignmentCenter;
    //    textField.attributedPlaceholder = [DTKTool AttributedStringWithStr:@"请填写" andNodeColor:KcolorHex(@"#787A79") andSeledColor:KcolorHex(@"#787A79") andFont:kFont(18) andPattenr:@""];
    [DTKTool YJBK:kwt(5) andView:textField andWidth:0.5 andColor:KcolorHex(@"#D9D9D9")];
    textField.font = kCFont(15);
    textField.keyboardType = UIKeyboardTypeDecimalPad;
    self.textField = textField;
    textField.delegate = self;
    textField.textColor = KcolorHex(@"#222222");
    [self addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(reduceBtn.mas_right).offset(kwt(2));
        make.right.equalTo(plusBtn.mas_left).offset(kwt(-2));
        make.centerY.offset(0);
        //        make.width.offset(kwt(100));
        make.height.offset(kwt(23));
        
    }];
//    [textField addTarget:self action:@selector(textFieldDidChangeValue:)  forControlEvents:UIControlEventEditingChanged];
    
}


- (void)setModel:(TemplateFoundDetailsTaskItemModelListModel *)model{
    _model = model;
//    self.textField.text = [NSString stringWithFormat:@"%ld", (long)model.number];
    if(model.degree > 0){///精度
        self.textField.keyboardType = UIKeyboardTypeDecimalPad;
    } else {
        self.textField.keyboardType = UIKeyboardTypeNumberPad;
    }
    self.plusBtn.enabled = (self.model.number.floatValue < self.model.numberMax);
    self.reduceBtn.enabled = (self.model.number.floatValue > self.model.numberMin);
    NSString * str = [NSString stringWithFormat:@"%@",self.model.number];
    NSArray <NSString *>* array = [str componentsSeparatedByString:@"."];
    if(array.count > 1){
        if(array[1].length > self.model.degree){
            str = [NSString stringWithFormat:@"%@.%@",array.firstObject,[array.lastObject substringToIndex:self.model.degree]];
        } else if(array[1].length < self.model.degree){
            str = [NSString stringWithFormat:@"%@%@",str,[@"00000000" substringToIndex:self.model.degree-array[1].length]];
        }
        if(self.model.degree == 0){
            str = array.firstObject;
        }
    } else {
        if(self.model.degree > 0){
            str = [NSString stringWithFormat:@"%@.%@",array.firstObject,[@"00000000" substringToIndex:self.model.degree]];
        }
    }
    self.textField.text = str;
}
///减
- (void)reduceBtnClick{
    self.model.number = [NSNumber numberWithFloat:self.model.number.floatValue - self.model.stepValue ];
    if([self SJPD:self.model.number.floatValue]){
        if(self.openReloadBlock){
            self.openReloadBlock();
        }
    }
}
///加
- (void)plusBtnClick{
    self.model.number = [NSNumber numberWithFloat:self.model.number.floatValue + self.model.stepValue ];
    if([self SJPD:self.model.number.floatValue]){
        if(self.openReloadBlock){
            self.openReloadBlock();
        }
    }
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString * str = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSArray <NSString *>* array = [str componentsSeparatedByString:@"."];
    if(array.count > 1){
        if(array.count > 2) {
            return NO;
        }
        if(array[1].length > self.model.degree){
            return NO;
        }
    }
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if([self SJPD:textField.text.floatValue]){
        self.model.number = [NSNumber numberWithFloat:textField.text.floatValue];
        self.plusBtn.enabled = (self.model.number.floatValue < self.model.numberMax);
        self.reduceBtn.enabled = (self.model.number.floatValue > self.model.numberMin);
        if(self.openReloadBlock){
            self.openReloadBlock();
        }
    }
}
//- (void)textFieldDidChangeValue:(UITextField *)sender{
//    if([self SJPD:sender.text.floatValue]){
//        self.model.number = [NSNumber numberWithFloat:sender.text.floatValue];
//    }
//}
///判断数据
- (BOOL)SJPD:(CGFloat)num{
    if(num > self.model.numberMax){
        self.model.number = [NSNumber numberWithFloat: self.model.numberMax];
        self.textField.text = [NSString stringWithFormat:@"%@",self.model.number];
        if(self.openHUDBlock){
            self.openHUDBlock(@"不能更大了");
            [self.textField resignFirstResponder];
            return NO;
        }
    } else if(num < self.model.numberMin){
        self.model.number = [NSNumber numberWithFloat: self.model.numberMin];
        self.textField.text = [NSString stringWithFormat:@"%@",self.model.number];
        if(self.openHUDBlock){
            self.openHUDBlock(@"不能更小了");
            [self.textField resignFirstResponder];
            return NO;
        }
    }
    return YES;
    
}
@end
